package cn.toesbieya.jxc.controller;

import cn.toesbieya.jxc.model.vo.R;
import cn.toesbieya.jxc.service.FileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.UUID;

@RestController
@RequestMapping("file")
public class FileController {

    @Resource
    private FileService service;

    @Value("${path.demo}")
    private String basePath;

    @GetMapping("delete")
    public R delete(@RequestParam String url) throws UnsupportedEncodingException {
        service.delete(URLDecoder.decode(url, "utf-8"));
        return R.success("删除成功");
    }

    @PostMapping("deleteBatch")
    public R deleteBatch(@RequestBody String[] urls) {
        if (urls.length == 0) {
            return R.fail("参数错误");
        }
        service.deleteBatch(urls);
        return R.success("批量删除成功");
    }


    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) {

        //原始文件名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用UUID重新生成文件名，防止文件名重复造成文件覆盖
        String fileName = UUID.randomUUID().toString() + suffix;
        File dir = new File(basePath);
        if (!dir.exists()) {
            //目录不存在,需要创建
            dir.mkdir();
        }
        try {
            //将临时文件转存到指定位置
            file.transferTo(new File(basePath + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.success(fileName);
    }

    @PostMapping("/uploadImg")
    public R uploadImg(@RequestParam("file") MultipartFile file) {
        //原始文件名
        String originalFilename = file.getOriginalFilename();
        String suffix = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用UUID重新生成文件名，防止文件名重复造成文件覆盖
        File dir = new File(basePath);
        if (!dir.exists()) {
            //目录不存在,需要创建
            dir.mkdir();
        }
        try {
            //将临时文件转存到指定位置
            file.transferTo(new File(basePath + suffix));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.success(suffix);
    }



    @GetMapping("/download")
    public void download(String name, HttpServletResponse response) {
        //输入流,通过输入流读取文件内容,
        try {
            FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("image/jpeg");
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
                outputStream.flush();
            }
            outputStream.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
